package com.fhx.core.base
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Looper
import android.os.SystemClock
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import com.fhx.core.R
import com.fhx.core.utils.*
abstract class BaseActivity : AppCompatActivity() {
    protected var mContext: Context? = null
    protected var rootView: ViewGroup? = null
    protected var mProgressView: View? = null

    protected var saveInstance: Boolean = false

    private var mActivityJumpTag //activity跳转tag
            : String? = null
    private var mClickTime //activity跳转时间
            : Long = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        EventBusManager.register(this)

//        val language = SpUtil.getInstance(this).getString(SpUtil.LANGUAGE)
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
//            LanguageUtil.changeAppLanguage(this, language)
//        }

//        val nightMode: Int = SharePreferencesUtil.getInteger(this, "nightmode", 0)
//        if (nightMode == 0) {
//            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
//            setStatusBarRealColor(ContextCompat.getColor(mContext!!,R.color.color_night))
//            setStatusBarTextColor(true)
//        } else {
//            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
//            setStatusBarRealColor(ContextCompat.getColor(mContext!!,R.color.white))
//            setStatusBarTextColor(false)
//        }
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        setStatusBarRealColor(ContextCompat.getColor(mContext!!,R.color.white))
        setStatusBarTextColor(false)

        val resId = getLayoutId()
        if (resId > 0) {
            setContentView(getLayoutId())
        }
        ActivityStackManager.add(this)
        findViewById<ImageView>(R.id.back)?.setOnClickListener {
            finish()
        }
        main()

        findViewById<View>(android.R.id.content)?.setOnClickListener {
            //在基类统一隐藏键盘
            SoftKeyboardUtils.hide(this)
        }
    }

    private fun initProgressView() {
        if (mProgressView == null) {
            mProgressView = layoutInflater.inflate(R.layout.loading_layout, rootView, false)
            mProgressView?.setOnClickListener(null)
            showProgress(false)
            rootView?.addView(mProgressView)
        }
    }

    fun showProgress(show: Boolean) {
        mProgressView?.visibility = if (show) View.VISIBLE else View.GONE
    }

    override fun setContentView(view: View?) {
        super.setContentView(view)
        rootView = window.decorView as ViewGroup
        initProgressView()
    }

    override fun setContentView(layoutResID: Int) {
        super.setContentView(layoutResID)
        rootView = window.decorView as ViewGroup
        initProgressView()
    }

    abstract fun getLayoutId(): Int

    override fun setTitle(titleId: Int) {
        title = getString(titleId)
    }

    override fun setTitle(title: CharSequence) {
        val titleView = findViewById<TextView>(R.id.title_tv)
        titleView?.text = title
    }


    open fun main() {

    }



    /**
     * 状态栏颜色
     */
    private fun setStatusBarRealColor(color: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = color
        }
    }

    /**
     * 状态栏文字颜色
     */
    private fun setStatusBarTextColor(useDart:Boolean){
        if (useDart) {
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_VISIBLE
        } else {
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_VISIBLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityStackManager.remove(this)
        EventBusManager.unregister(this)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        if (saveInstance) {
            super.onSaveInstanceState(outState)
        }
    }




    //重写startActivityForResult防止多次点击跳转activity
    override fun startActivityForResult(intent: Intent?, requestCode: Int, options: Bundle?) {
        if (checkDoubleClick(intent)){
            super.startActivityForResult(intent, requestCode, options)
        }
    }
    /**
     * 检查是否重复跳转，不需要则重写方法并返回true
     */
    protected open fun checkDoubleClick(intent: Intent?): Boolean {

        // 默认检查通过
        var result = true
        // 标记对象
        val tag: String?
        tag = if (intent?.component != null) { // 显式跳转
            intent.component!!.className
        } else if (intent?.action != null) { // 隐式跳转
            intent.action
        } else {
            return true
        }
        if (tag == mActivityJumpTag && mClickTime >= SystemClock.uptimeMillis() - 500) {
            // 检查不通过
            result = false
        }

        // 记录启动标记和时间
        mActivityJumpTag = tag
        mClickTime = SystemClock.uptimeMillis()
        return result
    }

}